package com.lisp.slf4jp.child_first;

import java.net.URL;
import java.net.URLClassLoader;

public class ChildFirstClassLoader extends URLClassLoader {

	public ChildFirstClassLoader(URL[] urls) {
		super(urls);
	}

	public ChildFirstClassLoader(URL[] urls, ClassLoader parent) {
		super(urls, parent);
	}

	public Class<?> loadClass(String name) throws ClassNotFoundException {
		Class<?> cached = findLoadedClass(name);
		if (cached != null) {
			return cached;
		}
		
		if(name.endsWith("IBox")) {  // IBox这个接口类，直接代理给AppClassLoader加载
			return super.loadClass(name);
		}
		
		// 优先自己加载，加载不到才代理给父classloader
    	try {
    		Class<?> selfLoad = findClass(name);
    		if(selfLoad != null) {
    			return selfLoad;
    		}
    	} catch(ClassNotFoundException cnfe) {
    		// ignore
    	}	    	
		
		return super.loadClass(name);
	}
}
